package com.faf.ui;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.faf.bean.Food;
import com.faf.bean.Orders;
import com.faf.dao.impl.DetailDaoImpl;
import com.faf.dao.impl.FoodDaoImpl;
import com.faf.dao.impl.OrdersDaoImpl;
import com.faf.dao.impl.StoreDaoImpl;
import com.faf.dao.impl.UserDaoImpl;

/**
 * Servlet implementation class BuyNow
 */
@WebServlet("/BuyNow")
public class BuyNow extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BuyNow() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8"); 
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		int fid = Integer.parseInt(request.getParameter("id").toString()); 
		int fnum = Integer.parseInt(request.getParameter("num"));
		
		
		FoodDaoImpl foodDao = new FoodDaoImpl();
		int sid = 0;
		try {
			sid = foodDao.getFoodByFoodID(fid).getStoreId();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat simpleDateFormat_on = new SimpleDateFormat("sssHHMMmmyyyydd");
		String orderNumber = simpleDateFormat_on.format(new Date(System.currentTimeMillis()));
		
		UserDaoImpl userDao = new UserDaoImpl();
		int userId = 0;
		try {
			userId = userDao.getUserByNumber(request.getSession().getAttribute("account").toString()).getUserId();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return;
		}
		
		//int ordersId, String ordersNumber, int userId, int storeId, int ordersState, String ordersTime, String messageRemark
		// ordersNumber, userId, storeId, ordersState, ordersTime, messageRemark
		Orders order = new Orders(
				-1,
				orderNumber,
				userId,
				sid,
				-1,
				simpleDateFormat.format(new Date(System.currentTimeMillis())),
				null);
		
		OrdersDaoImpl orderDao = new OrdersDaoImpl();
		
		try {
			orderDao.insert(order);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		
		// 初始化订单
		// int detailId, String orderNumber, int foodId, int foodNumber, String messageRemark
		com.faf.bean.Detail detail = new com.faf.bean.Detail(
				-1,
				orderNumber,
				fid,
				fnum,
				"");
		
		DetailDaoImpl detailDao = new DetailDaoImpl();
		detailDao.insert(detail);
		
		System.out.println(orderNumber);
		
		List<com.faf.bean.Detail> placeorder_data_detail = detailDao.getDetailListByorderNumber(orderNumber);
		HashMap<Integer, Food> placeorder_data_foodmap = new HashMap<Integer, Food>();
		
		Food food = null;
		try {
			food = foodDao.getFoodByFoodID(fid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		
		placeorder_data_foodmap.put(fid, food);
		
		
		request.setAttribute("orderNumber", orderNumber);
		request.setAttribute("placeorder_data_detail", placeorder_data_detail);
		request.setAttribute("placeorder_data_foodmap", placeorder_data_foodmap);
		
		request.getRequestDispatcher("/UserGlobal/place_order.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
